var id = "";
var idss = getvl("idss"); //getvl("idss");
chaxundingdanxinxi();
var num = 0;
var jinge = 0;
var shopinfoid = "";
function chaxundingdanxinxi() {
	$("#shangpingxinxi").html("");
	$.ajax({
		url: urlcore + "/api/goodsCar/shoppingtrolley",
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success: function(data) {
			if(data.success == true) {
				var shangpingxinxi = "";
				   if(data.data!=null){
				$.each(data.data, function(y, n) {
					shopinfoid = n.id;
                 	$.each(n.goodsList, function(w, q) {
					shangpingxinxi += '<input type="text" name="goodsids" style="display: none;"   value="' + q.id + '"/><div class="sell_daike_shang" >' +
						'		<div class="clearfix">' +
						'			<div class="clearfix tuwen">' +
						'		        <div class="tuwen-img fl">' +
						'		            <img onerror="errorImg(this)" src="' + q.coverUrl + '" width="75" height="75">' +
						'		        </div>' +
						'		        <div class="tuwen-wenzi">' +
						'		            <h5 class="wuye_h01">' + q.goodsTitle + '</h5>' +
						'		            <div class="wuye_div02">&yen;' + q.tradePrice + '-&yen;' + q.packPrice + '</div>' +
						'		            <a href="javascript:shanchu(' + q.id + ');" class="daike_xiugai">移除</a>' +
						'		        </div>' +
						'		    </div>' +
						'		</div>';
						
					$.each(q.myStockList, function(m, k) {

						shangpingxinxi +=
							'	    <div class="dingdan_fenlei clearfix goodssoket' + q.id + '" name="' + k.saleNum + '" id="' + k.id + '">' +
							'	    	<ul class="fl">' +
							'	    		<li>颜色：<span>' + k.colour.colourName+ '</span></li>' +
							'	    		<li>尺码：<span>' + k.size.sizeName + '</span></li>' + /*'+a.size.sizeName+'*/
							'	    		<li>单价：<span>' + q.goodsCar.packRate + '元/件</span></li>' +
							'	    	</ul>' +
							'	    	<div class="fr">' +
							'	    		<h5>' + ((q.goodsCar.packRate*k.saleNum).toFixed(2)) + '元</h5>' +
							'	    		<h6  class="jianshu' + q.id + '" name="' + k.saleNum + '">' + k.saleNum+'件</h6>' +
							'	    	</div>' +
							'	    </div>';
						num += k.saleNum;
						jinge += k.saleNum * q.goodsCar.packRate;
					});

					shangpingxinxi += '</div>';
					});
					
					
					
				});
			}
				$("#zhongjia").text(jinge.toFixed(2));
				$("#zhongjias").val(jinge.toFixed(2));
				$("#jianshu").text("共" + num + "件");
				$("#jiayunfei").text(jinge.toFixed(2));
				$("#oderzongmingxi").val(jinge.toFixed(2));
				$("#shangpingxinxi").append(shangpingxinxi);
			}
		},
		error: function() {
			/* Act on the event */
			alert("error");
		}
	});
	if(sessionStorage.getItem("dizhiid") != "" && sessionStorage.getItem("dizhiid")!=null) {
		xinzhengdizhi(sessionStorage.getItem("dizhiid"));
	} else{
		dizhi();
	}

}
var dihziid = "";
function xinzhengdizhi(sizhi) {
	$("#dizhi").html(""); //地址信息
	$.ajax({
		url: urlcore + "/api/address/selectOne?id=" + sizhi,
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success: function(data) {
			if(data.success == true) {
				var da = data.data;
				var dizhi = "";
				if(da != null) {
					dihziid = da.id;
							var  zifuchuandizhi="";
 					var zifuchuan=da.province+da.city+da.county+da.address;
 		if(zifuchuan.length>12){
          	zifuchuandizhi+=zifuchuan.substr(0 , 14)+"...";
          }else{
          	zifuchuandizhi+=zifuchuan;
          }
					dizhi += '<div class="detail_dizhi clearfix" style="margin-top: 0;">' +
						'<ul class="clearfix"><li><span>收&nbsp;&nbsp;货&nbsp;&nbsp;人</span>' + da.userName + '</li>' +
						'        <li class="a_xiugai_right">' +
						'            <span>电&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;话</span>' + da.userPhone + '</li>' +
						'        <li><span>收货地址</span>' +zifuchuandizhi+ '</li>' +
						'    </ul>' +
						'    <a href="../11.2.3dizhi.html?id=' + da.userId + '">修改</a>'
					'</div>';
				} else {
					 dizhi += '<div class="daike_dizhi">' +
						'	<a href="../11.2.1dizhi_add.html">' +
						'		<h6>填写收货地址</h6>' +
						'		<p>完善客户的收货地址信息，才能生成订单</p>' +
						'	</a>' +
						'</div>';
				}
				$("#dizhi").append(dizhi); //地址信息
			}else{
				alert(data.msg)
			}
		},
		error: function() {
			/* Act on the event */
			alert("error");
		}
	});

}

function dizhi() {
	$.ajax({
		url: urlcore + "/api/address/selectDefault",
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success: function(data) {
			//	alert(data.data);
			if(data.success == true) {
				var da = data.data;
				var dizhi = "";
				if(da != null) {
					dihziid = da.id;
							var  zifuchuandizhi="";
 					var zifuchuan=da.province+da.city+da.county+da.address;
 		if(zifuchuan.length>12){
          	zifuchuandizhi+=zifuchuan.substr(0 , 14)+"...";
          }else{
          	zifuchuandizhi+=zifuchuan;
          }
					dizhi += '<div class="detail_dizhi clearfix" style="margin-top: 0;">' +
						'<ul class="clearfix"><li><span>收&nbsp;&nbsp;货&nbsp;&nbsp;人</span>' + da.userName + '</li>' +
						'        <li class="a_xiugai_right">' +
						'            <span>电&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;话</span>' + da.userPhone + '</li>' +
						'        <li><span>收货地址</span>' +zifuchuandizhi+ '</li>' +
						'    </ul>' +
						'    <a href="../11.2.3dizhi.html?id=' + da.userId + '">修改</a>'
					'</div>';
				} else {
					var dizhi = '<div class="daike_dizhi">' +
						'	<a href="../11.2.1dizhi_add.html">' +
						'		<h6>填写收货地址</h6>' +
						'		<p>完善客户的收货地址信息，才能生成订单</p>' +
						'	</a>' +
						'</div>';
				}
				$("#dizhi").append(dizhi); //地址信息
			}
		},
		error: function() {
			/* Act on the event */
			alert("error");
		}
	});
}

function shanchu(te) {
		$.ajax({
			url: urlcore + "/api/goodsCar/delGoodsCarByGoodsIdOnPC?ids=" +te,
			type: "get",
			dataType: 'json',
			contentType: "application/json;charset=utf-8",
			success: function(data) {
				if(data.success == true) {
                   location.reload(); 
				}else{
					alert(data.msg);
				}
			},
			error: function() {
				alert("error");
			}
		});
	

}

var shan = "";
var yunfei = "0";

function xiugai() {
	shan = $("#shangping").val(jinge.toFixed(2));
	yunfei = $("#yunfei").val();
}

function xiugaijiage() {

	var shan = (parseFloat($("#shangping").val()) + parseFloat($("#yunfei").val())).toFixed(2);
	
	$("#jiayunfei").html("&yen;" + shan);
$("#oderzongmingxi").val(shan);

   
		    $(".sell-xiugai1").hide();
        $("#mask").remove();
}

function shengchengdingdan() {
	//var shopinfo = new Array();
	if(uuuuid!=""){
		
	
	var shopinfogoodsid="";
	var zhongjias = $("#zhongjias").val();
	var jiayunfei = $("#oderzongmingxi").val();
	var xingbie = $(".xingbie").html();
	var peishong = 0;
      if(xingbie == "请选择") {
		//alert("未选择配送类型");
	} else if(xingbie == "快递") {
		peishong = 2;
	} else if(xingbie == "包邮") {
		$("#yunfei").val("0");
		peishong = 1;
	} else if(xingbie == "物流到付") {
		$("#yunfei").val("0");
		peishong = 3;
	}

	var sh = '[';
	var goodsStockId = '[';
	$('input[name="goodsids"]').each(function(y, n) {
		var goodssid = $(this).val();
		//shopinfo.push(goodssid);
		shopinfogoodsid+=goodssid+",";
	
		$(".goodssoket" + goodssid).each(function() {
			var guigeid = $(this).attr("id");
			var jianshu = $(this).attr("name"); //规格id
			if(y == 0) {
				goodsStockId += '{"id":' + guigeid + ',"saleNum":' + jianshu + '}';
			} else {
				goodsStockId += ',{"id":' + guigeid + ',"saleNum":' + jianshu + '}';
			}
		}); //规格id

	});
	goodsStockId += ']';
	         if($("#yunfei").val()==""){
	        sh += '{"shopId":' + shopinfoid + ',"freightMoney":' +0+ ',"payMoney":' + jiayunfei + ',"orderMoney":' + zhongjias + ',"goodsNumber":' + num + ',"giveType":' + peishong + ',"payType":' + 0 + ',"addressId":' + dihziid + ',"goodsId":"' + shopinfogoodsid + '","goodsStockId":'+goodsStockId+'}';
	         }else{
	      	sh += '{"shopId":' + shopinfoid + ',"freightMoney":' +$("#yunfei").val()+ ',"payMoney":' + jiayunfei + ',"orderMoney":' + zhongjias + ',"goodsNumber":' + num + ',"giveType":' + peishong + ',"payType":' + 0 + ',"addressId":' + dihziid + ',"goodsId":"' + shopinfogoodsid + '","goodsStockId":'+goodsStockId+'}';
	         }
sh += ']';

	
	if(peishong !=0) {
	  	$.ajax({
			url: urlcore + "/api/order/addValet4?userId="+uuuuid,
			type: "post",
			contentType: "application/json;charset=utf-8",
			data: JSON.stringify({
				"myOrder": sh
			}),
			dataType: "json",
			success: function(data) {
				if(data.success == true) {
					shanchuss(shopinfogoodsid);
				}else{
					alert(data.msg);
				}
			},
			error: function() {
				alert("error");
			}
		});
	  
	}else {
		alert("请选择邮寄方式");
	}
		
		
	
	}else{
		alert("未登录/或登录超时");
	}
}


function shanchuss(shopinfogoodsid) {

		$.ajax({
			url: urlcore + "/api/goodsCar/delGoodsCarByGoodsIdOnPC?ids=" + shopinfogoodsid,
			type: "get",
			dataType: 'json',
			contentType: "application/json;charset=utf-8",
			success: function(data) {
				if(data.success == true) {
					alert("订单生成，到历史订单里面观看");
					 location.reload(); 
				}else{
					alert(data.msg);
				}
				/*console.log(goodslist);*/
			},
			error: function() {
				alert("error");
			}
		});
	

}